/**
 * 
 */
package com.quiz.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.quiz.model.Player;


/**
 *
 * @author Brian Boyle
 * Apr 10, 2012
 */
public class PlayerDatasource extends QuizDataSource {

    /**
     * @param context
     */
    public PlayerDatasource(Context context) {
        super(context);
    }

    public Player getPlayerDetails() {

        Cursor cursor = getDatabase().query(Player.TABLE_NAME, null, null, null, null, null, null);
        Player player = convertCursorToPlayer(cursor);
        cursor.close();
        return player;
    }

    public void updatePlayerLivesAndStatus(Player player) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(Player.COLUMN_LIVES, player.getLives());
        contentValues.put(Player.COLUMN_STATUS, player.getStatus());
        getDatabase().update(Player.TABLE_NAME, contentValues, null, null);
    }

    public void updatePlayer(Player player) {

        ContentValues contentValues = new ContentValues();
        contentValues.put(Player.COLUMN_LIVES, player.getLives());
        contentValues.put(Player.COLUMN_STATUS, player.getStatus());
        contentValues.put(Player.COLUMN_HIGHSCORE, player.getHighScore());
        contentValues.put(Player.COLUMN_CURRENT_RANK, player.getCurrentRank());
        getDatabase().update(Player.TABLE_NAME, contentValues, null, null);
    }

    private Player convertCursorToPlayer(Cursor cur) {
        cur.moveToFirst();
        return new Player(cur.getInt(0), cur.getInt(1), cur.getString(2), cur.getString(3));
    }

}
